var express = require('express');
var router = express.Router();
const superagent = require('superagent');
const cheerio = require('cheerio');
const async = require('async');
const fs = require('fs');
const url = require('url');
let parse = '%E5%8A%A8%E6%BC%AB' //'%E7%BE%8E%E8%85%BF' //mt
let tiebaUrl  = 'https://tieba.baidu.com/f?kw='+parse+'&ie=utf-8'
// let tiebaUrl  ='https://tieba.baidu.com/f?ie=utf-8&kw=%E5%8A%A8%E6%BC%AB'
let arr = []
// 动漫// https://tieba.baidu.com/f?ie=utf-8&kw=%E5%8A%A8%E6%BC%AB
let page = 100
for(let i = 0; i < page; i++){
  
  if(i!=0){
    tiebaUrl = 'https://tieba.baidu.com/f?ie=utf-8&kw='+parse+'&pn='+i*50
  }

  superagent.get(tiebaUrl).end(function(err, res){
    if(err){
      return console.error(err)
    }

    let $ = cheerio.load(res.text);
    $('div.threadlist_title>a.j_th_tit').each(function(index, element){
      let $element = $(element);
      let href = url.resolve(tiebaUrl, $element.attr('href'));
      superagent.get(href).end(function(err, res){
        if(err){
          return console.error(err);
        }
        let $ = cheerio.load(res.text);
        let add = href;
        let imgUrlArr = []
        let title = $('div#j_core_title_wrap>h3.core_title_txt').attr('title')
        // console.log(title)
        let num = $('ul>li.l_reply_num>span.red').html()
        
        $('img.BDE_Image').each(function(idx, element){
          let $element = $(element);
          let imgUrl  = url.resolve(href, $element.attr('src'));
          imgUrlArr.push(imgUrl)
        })
        arr.push({
          'pic': imgUrlArr
        });
        let stad = {
          // "address": add,
          "pic": imgUrlArr,
        };
        fs.appendFile('data/result.json', JSON.stringify(stad)+',' ,'utf-8', function (err) {
            if(err) throw new Error("appendFile failed...");
            //console.log("数据写入success...");
        });
      })
    })
  })

}
function GetUrlParam(paraName) {
　　　　var url = document.location.toString();
　　　　var arrObj = url.split("?");

　　　　if (arrObj.length > 1) {
　　　　　　var arrPara = arrObj[1].split("&");
　　　　　　var arr;

　　　　　　for (var i = 0; i < arrPara.length; i++) {
　　　　　　　　arr = arrPara[i].split("=");

　　　　　　　　if (arr != null && arr[0] == paraName) {
　　　　　　　　　　return arr[1];
　　　　　　　　}
　　　　　　}
　　　　　　return "";
　　　　}
　　　　else {
　　　　　　return "";
　　　　}
　　}

// http://tieba.baidu.com/f?kw=%E6%9D%9C%E6%B5%B7%E6%B6%9B&ie=utf-8&pn=1000

/* GET users listing. */
router.get('/', function(req, res, next) {
  res.send('respond with a resource');
});

module.exports = router;
